package com.qs.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;

/**
 * @Desc:  服务降级
 * @author: jianfeng
 * @Time: 2019/9/26 15:51
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")//默认服务降级
public class HystrixController {

    //超时配置
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "30000")
//    })
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//设置熔断
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//断路器打开最小请求次数，这里表示10次
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//断路器半开状态（半熔断），10秒以内
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")//请求异常次数的百分比，这里表示10次请求，6次异常
//    })
    @HystrixCommand
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number") Integer number){
        if (number%2==0){
            return "success";
        }
        RestTemplate restTemplate=new RestTemplate();
        return restTemplate.postForObject("http://localhost:8081/product/listForOrder",
                Arrays.asList("157875196366160022"),
                String.class);

    }

    private String fallback(){
        return "太拥挤了，请稍后再试~~";
    }

    /**
     * @Desc:  默认服务降级方法
     * @author: jianfeng
     * @Time: 2019/9/27 9:11
     *
     * @return java.lang.String
     */
    private String defaultFallback(){
        return "默认提示：太拥挤了，请稍后再试~~";
    }

}
